package com.nut.service;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-05-09 21:23
 * @ Description : 自定义access实现类
 */

@Service
public class MyServiceImpl implements MyService
{
    @Override
    public boolean hasPermission(HttpServletRequest httpServletRequest,
                                 Authentication authentication)
    {
        String uri = httpServletRequest.getRequestURI();
        Object principal = authentication.getPrincipal();
        if(principal instanceof UserDetails)
        {
            UserDetails userDetails = (UserDetails) principal;
            Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
            return authorities.contains(new SimpleGrantedAuthority(uri));
        }
        return false;
    }
}
